import {
  asyncRouterMap,
  constantRouterMap
} from 'src/router'
import { fetchAll } from 'api/admin/menu/index';
/**
 * 通过authority判断是否与当前用户权限匹配
 * @param menus
 * @param route
 */
function hasPermission(menus, route) {
  // console.log(route);
  if (route.authority) {
    if (menus[route.authority] !== undefined) {
      // console.log(menus);
      // console.log(menus[route.authority]);
      return menus[route.authority];
    } else {
      // console.log(menus);
      return false;
    }
  } else {
    return true
  }
}

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param asyncRouterMap
 * @param roles
 */
function filterAsyncRouter(asyncRouterMap, menus, menuDatas) {
  const accessedRouters = asyncRouterMap.filter(route => {
    if (hasPermission(menus, route)) {
      if(localStorage.getItem('locale') == 'zh_cn'){
        route.name = menuDatas[route.authority].title;
      }else if(localStorage.getItem('locale') == 'zh_tw'){
        route.name = menuDatas[route.authority].attr7;
      }else {
        route.name = menuDatas[route.authority].attr8;
      }
      route.icon = menuDatas[route.authority].icon;
      
      // if( localStorage.getItem('userType') == '5' ){
      //   if( route.name == '人员管理' || route.name == '人員管理' || route.name == "Personnel" ){
      //     route = {};
      //     return;
      //   } 
      // }

      if( localStorage.getItem('userType') == '2' && localStorage.getItem('grade') !== '1' ){
        if( route.name == '分润' || route.name == "分潤" || route.name == 'Profit'  || route.name == '资金管理' || route.name == "資金管理" || route.name == 'Fund' ){
          route = {};
          return;
        } 
      }

      if(localStorage.getItem('companyId') == '2'){
        if( route.name == '广告' || route.name == "待审核兑现" || route.name == "廣告管理" || route.name == "待審批兌現" || route.name == "Advertisement" || route.name == "Withdrawal" || route.name == '公司' || route.name == "Company" || route.name == '公司审核' || route.name == "公司審批" || route.name == '兑现' || route.name == "兌現" || route.name == 'Withdrawal' || route.name == '分润' || route.name == "分潤" || route.name == 'Profit' ){
          route = {};
          return;
        } 
        // if( localStorage.getItem('userType') == '2' ){
        //   if( route.name == '审核管理' || route.name == '设备管理' || route.name == "審批管理" || route.name == "設備管理" || route.name == "Approval" || route.name == "Device" ){
        //     route = {};
        //     return;
        //   } 
        // }

        // if( route.name == "设备管理"  || route.name == "設備管理"  || route.name == "Device" ){
        //   if( localStorage.getItem('phone') == '13501189583' ||  localStorage.getItem('phone') == '15018955912' || localStorage.getItem('phone') == '15219441946' ){
        //     route = {};
        //     return;
        //   }
        // } 
      }

      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children, menus, menuDatas);
      }
      return true
    }
    return false
  })
  // console.log(menus);
  // console.log(accessedRouters);
  return accessedRouters
}

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes({ commit }, menus) {
      return new Promise(resolve => {
        // const accessedRouters = filterAsyncRouter(asyncRouterMap, menus);
        // commit('SET_ROUTERS', accessedRouters);
        // resolve();
        fetchAll().then(data => {
          const menuDatas = {};
//        console.log(data)
          for (let i = 0; i < data.length; i++) {
            menuDatas[data[i].code] = data[i];
//          console.log('菜单')
//          console.log(menuDatas)
          }
          const accessedRouters = filterAsyncRouter(asyncRouterMap, menus, menuDatas);
          commit('SET_ROUTERS', accessedRouters);
          resolve();
        });
      })
    }
  }
};

export default permission;
